local includes = {
	SDL_h = 1
}

local depends = {
	SDL_Event = {
		'SDL_AudioDeviceEvent',
		'SDL_CameraDeviceEvent',
		'SDL_ClipboardEvent',
		'SDL_CommonEvent',
		'SDL_DisplayEvent',
		'SDL_DropEvent',
		'SDL_GamepadAxisEvent',
		'SDL_GamepadButtonEvent',
		'SDL_GamepadDeviceEvent',
		'SDL_GamepadSensorEvent',
		'SDL_GamepadTouchpadEvent',
		'SDL_JoyAxisEvent',
		'SDL_JoyBallEvent',
		'SDL_JoyBatteryEvent',
		'SDL_JoyButtonEvent',
		'SDL_JoyDeviceEvent',
		'SDL_JoyHatEvent',
		'SDL_KeyboardDeviceEvent',
		'SDL_KeyboardEvent',
		'SDL_MouseButtonEvent',
		'SDL_MouseDeviceEvent',
		'SDL_MouseMotionEvent',
		'SDL_MouseWheelEvent',
		'SDL_PenAxisEvent',
		'SDL_PenButtonEvent',
		'SDL_PenMotionEvent',
		'SDL_PenProximityEvent',
		'SDL_PenTouchEvent',
		'SDL_PinchFingerEvent',
		'SDL_QuitEvent',
		'SDL_RenderEvent',
		'SDL_SensorEvent',
		'SDL_TextEditingCandidatesEvent',
		'SDL_TextEditingEvent',
		'SDL_TextInputEvent',
		'SDL_TouchFingerEvent',
		'SDL_UserEvent',
		'SDL_WindowEvent',
	},
	SDL_GPUColorTargetInfo = {
		'SDL_FColor',
	},
	SDL_GPUColorTargetDescription = {
		'SDL_GPUColorTargetBlendState',
	},
	SDL_GPUGraphicsPipelineCreateInfo = {
		'SDL_GPURasterizerState',
		'SDL_GPUMultisampleState',
		'SDL_GPUDepthStencilState',
		'SDL_GPUStencilOpState',
	},
}

local f = io.popen 'dir /b/s src\\main\\java\\*.java'
local s = f:read '*a'
f:close()
local n = 0
for line in s:gmatch '%C+' do
	-- print(line)
	f = io.open(line, 'rb')
	s = f:read '*a'
	f:close()
	s = s:gsub('/%*.-%*/', '')--:gsub('//[^\n]*', '')
	for word in s:gmatch '[%w_]+' do
		if word:find '^SDLK?_' and not includes[word] then
			includes[word] = 0
			n = n + 1
		end
	end
end
for name in pairs(includes) do
	if depends[name] then
		for _, word in ipairs(depends[name]) do
			if not includes[word] then
				includes[word] = 0
				n = n + 1
			end
		end
	end
end
print('found ' .. n .. ' symbols in java files')

f = io.open('includes.txt', 'rb')
s = f:read '*a'
f:close()

n = 0
local lines = {}
for line in s:gmatch '(.-)\r?\n' do
	local name = line:match '^%s*%-%-include%-.-%s+([%w_]+)'
	if name then
		if includes[name] then
			includes[name] = includes[name] + 1
			lines[#lines + 1] = '  ' .. line:gsub('^%s+', '')
			n = n + 1
		else
			lines[#lines + 1] = '# ' .. line:gsub('^%s+', '')
		end
	else
		name = line:match '^[#%s]+%-%-include%-.- ([%w_]+)'
		if name and includes[name] then
			includes[name] = includes[name] + 1
			lines[#lines + 1] = line:gsub('^[#%s]+', '  ')
			n = n + 1
		else
			lines[#lines + 1] = line
		end
	end
end
print('enable ' .. n .. ' symbols in includes.txt')

for name, n in pairs(includes) do
	if n == 0 then
		print('WARN: not found symbol in includes.txt: ' .. name)
	end
end

f = io.open('includes.txt', 'wb')
f:write(table.concat(lines, '\n'), '\n')
f:close()

print 'DONE!'
